#include "behaved.h"
#include "map.h"
#include "game.h"
#include "player.h"

void Behaved::Add( Buff* buff )
{
    if ( IsDestroyed() )
        throw std::runtime_error( "Buff added to destroyed behaved" );

    if ( buff == 0 )
        throw std::runtime_error( "Null buff added to behaved" );

    if ( HasBuff( buff ) )
        throw std::runtime_error( "Duplicate buff added to behaved" );

    _buffs.insert( buff );
}

void Behaved::Remove( Buff* buff )
{
    if ( IsDestroyed() )
        throw std::runtime_error( "Buff removed from destroyed behaved" );

    if ( buff == 0 )
        throw std::runtime_error( "Null buff removed from behaved" );

    if ( !HasBuff( buff ) )
        throw std::runtime_error( "Buff removed from unoccupied behaved" );

    _buffs.erase( buff );
}

bool Behaved::HasBuff( const Buff* buff ) const
{
    return _buffs.count( const_cast< Buff* >( buff ) ) != 0;
}

void Behaved::GetAllBuffs( BuffList& outputList )
{
    foreach ( Buff* b, _buffs )
        outputList.push_back( b );
    std::sort( outputList.begin(), outputList.end(), &Buff::PrioritySort );
}

void Behaved::GetAllBuffs( ConstBuffList& outputList ) const
{
    foreach ( const Buff* b, _buffs )
        outputList.push_back( b );
    std::sort( outputList.begin(), outputList.end(), &Buff::PrioritySort );
}

const Behaved* Behaved::GetBase() const
{
    return this;
}

Behaved* Behaved::GetBase()
{
    return this;
}

MessageBank& Behaved::Bank()
{
    return GetMap()->GetGame().Bank();
}

bool Behaved::IsPlayer() const
{
    return GetMap()->GetGame().GetPlayer() == this;
}

void Behaved::QueueMessage( const ustring& message, const IO::SymList& firstChar )
{
    GetMap()->GetGame().QueueMessage( message, firstChar );
}

void Behaved::QueueMessage( const std::string& message, const IO::SymList& firstChar )
{
    GetMap()->GetGame().QueueMessage( message, firstChar );
}

void Behaved::QueueMessage( const ustring& message, uint before )
{
    GetMap()->GetGame().QueueMessage( message, before );
}

void Behaved::QueueMessage( const std::string& message, uint before )
{
    GetMap()->GetGame().QueueMessage( message, before );
}

void Behaved::DisplayMessages()
{
    GetMap()->GetGame().DisplayMessages();
}